package com.qys.livesMall.app.user.controller.app;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qys.livesMall.app.user.model.dto.app.AppUserRechargeDTO;
import com.qys.livesMall.app.user.model.dto.app.AppUserWithdrawalsDTO;
import com.qys.livesMall.app.user.model.dto.rw.AuditRw;
import com.qys.livesMall.app.user.model.dto.sys.SysUserRechargePageDTO;
import com.qys.livesMall.app.user.model.entity.AppUserLevel;
import com.qys.livesMall.app.user.model.entity.H2AppUserRechargeWithdrawals;
import com.qys.livesMall.app.user.model.vo.app.WithdrawalsConfigVO;
import com.qys.livesMall.app.user.service.IH2AppUserRechargeWithdrawalsService;
import com.qys.livesMall.common.exception.QysException;
import com.qys.livesMall.common.result.R;
import com.qys.livesMall.common.utils.MessageUtils;
import com.qys.livesMall.finance.enums.AccountBusinessTypeEnum;
import com.qys.livesMall.framework.utils.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 用户充值 前端控制器
 * </p>
 *
 * @author dp
 * @since 2023-04-27
 */
@RestController
@Tag(name = "app_用户充值(提现)")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class H2AppUserRechargeWithdrawalsController {

    private final IH2AppUserRechargeWithdrawalsService userRechargeService;

    @PostMapping("/app/user/recharge")
    @Operation(summary = "用户充值")
    public R<Void> recharge(@RequestBody @Validated AppUserRechargeDTO rechargeDTO) {
        userRechargeService.recharge(rechargeDTO);
        return R.ok();
    }

    @PostMapping("/app/user/recharge/page")
    @Operation(summary = "分页查询用户充值记录")
    public R<Page<H2AppUserRechargeWithdrawals>> page(@RequestBody SysUserRechargePageDTO rechargePageDTO) {
        rechargePageDTO.setType((AccountBusinessTypeEnum.RECHARGE.getType()));
        rechargePageDTO.setUserId(String.valueOf(SecurityUtils.getUserId()));
        return R.ok(userRechargeService.page(rechargePageDTO));
    }

    @PostMapping("/app/user/withdrawals")
    @Operation(summary = "用户提现")
    public R<Void> withdrawals(@RequestBody @Validated AppUserWithdrawalsDTO withdrawalsDTO) {
        //密码校验
        if (StringUtils.isBlank(withdrawalsDTO.getPassword())) {
            throw new QysException(MessageUtils.message("password.error"));
        }
        userRechargeService.withdrawals(withdrawalsDTO);
        return R.ok();
    }

    @PostMapping("/app/user/withdrawals/page")
    @Operation(summary = "分页查询用户提现记录")
    public R<Page<H2AppUserRechargeWithdrawals>> withdrawalsPage(@RequestBody SysUserRechargePageDTO rechargePageDTO) {
        rechargePageDTO.setType(AccountBusinessTypeEnum.WITHDRAWAL.getType());
        rechargePageDTO.setUserId(String.valueOf(SecurityUtils.getUserId()));
        return R.ok(userRechargeService.page(rechargePageDTO));
    }

    /**
     * 取消
     * @param id
     * @return
     */
    @PostMapping("/app/user/rw/cancel/{id}")
    public R auditNoPass(@PathVariable("id") Long id){
        AuditRw auditRw =new AuditRw();
        auditRw.setId(id);
        auditRw.setType((byte) 3);
        return userRechargeService.auditNoPass(auditRw);
    }


    @GetMapping("/app/user/withdrawals/config")
    public R<WithdrawalsConfigVO> getWithdrawalsConfig(){
        return R.ok(userRechargeService.getWithdrawalsConfig(SecurityUtils.getUserId()));
    }
}
